package com.dopios.services.webapp.controller;

import com.dopios.services.persistance.domain.CityAdvice;
import com.dopios.services.persistance.domain.CityAdvicesForm;
import com.dopios.services.webapp.service.AdviceServiceImpl;
import com.dopios.services.webapp.service.AdviceService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Controller
@RequestMapping(value = "/view")
public class ViewModelServletController {

    private static final Logger logger = Logger.getLogger(ViewModelServletController.class);

    @Autowired
    @Qualifier("adviceService")
    AdviceService adviceService;

    @Autowired
    AdviceServiceImpl adviceServiceImpl;


     /* Serves the View for the advices of a specific city*/
    @RequestMapping(value = "advice/{city}", method = RequestMethod.GET)
    public ModelAndView detection( @PathVariable String city,Model model, HttpServletRequest request, HttpServletResponse response) {
        int authenticatedUserId= adviceService.getUserIdByUsername("dopios");//We hardcode it.Normally we would get it from the session after the user is authenticated.
        List<CityAdvice> cityAdvices =  adviceService.getCityAdvicesForCityUser(Integer.toString(authenticatedUserId),city);
        CityAdvicesForm form = new CityAdvicesForm();
        form.setCityAdvices(cityAdvices);
        ModelAndView  modelAndView = new ModelAndView("dopios/viewCityAdvice","cityAdvicesForm",form);
        modelAndView.addObject("ratingOptions", adviceService.getRatingOptions());
        return modelAndView;
        }

       /* Handles the form that contains the updated ratings*/
    @RequestMapping(value = "advice/processCityAdvicesForm", method = RequestMethod.POST)
    public ModelAndView processCityAdvicesForm( @ModelAttribute("cityAdvicesForm") CityAdvicesForm cityAdvicesForm,Object command, HttpServletRequest request, HttpServletResponse response) {
        int authenticatedUserId= adviceService.getUserIdByUsername("dopios");//We hardcode it.Normally we would get it from the session after the user is authenticated.

        List<CityAdvice> cityAdvices =  cityAdvicesForm.getCityAdvices();
        adviceService.updateAddUserAdvicesRatings(cityAdvices,authenticatedUserId);
        CityAdvicesForm form = new CityAdvicesForm();
        form.setCityAdvices(cityAdvices);
        ModelAndView  modelAndView = new ModelAndView("dopios/viewCityAdvice","cityAdvicesForm",form);
        modelAndView.addObject("ratingOptions", adviceService.getRatingOptions());
        return modelAndView;
    }
}
